//
//  QDdisComplicationVC.m
//  QuickDoctor
//
//  Created by 李伟 on 2016/12/23.
//  Copyright © 2016年 Eric. All rights reserved.
//

#import "QDdisComplicationVC.h"
#import "QDDisComplicationCell.h"
#import "NetworkData.h"
#import "QDDisComplicationModel.h"

@interface QDdisComplicationVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSArray<QDDisComplicationModel*> *modelList;
@property (nonatomic, weak) UITableView *tableView;
@end

@implementation QDdisComplicationVC


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadData];
    
    [self setupUI];
}

#pragma mark
#pragma mark - 重写set方法刷新数据
- (void)setModelList:(NSArray<QDDisComplicationModel *> *)modelList
{
    _modelList = modelList;
    [self.tableView reloadData];
}

#pragma mark
#pragma mark - 获取数据
- (void)loadData
{
    NetworkData *manager =[NetworkData sharedNetworkData];
    [manager getComplicationListWithCompleteBlock:^(id responseObject, NSError *error) {
        
        NSArray *tempArr = responseObject[@"data"];
        
        NSMutableArray *mArray = [NSMutableArray array];
        for (NSDictionary* dict in tempArr) {
            
            QDDisComplicationModel *model = [QDDisComplicationModel yy_modelWithDictionary:dict];
            
            [mArray addObject:model];
        }
        self.modelList = mArray.copy;
        
    }];
}


#pragma mark
#pragma mark - 搭建界面
- (void)setupUI
{
#pragma mark -- TableView
    UITableView *tableView = [[UITableView alloc]init];
    self.tableView = tableView;
    [self.view addSubview:tableView];
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    
    //行高
    tableView.estimatedRowHeight = 100;
    tableView.rowHeight = UITableViewAutomaticDimension;
    
    //代理/数据源
    tableView.delegate = self;
    tableView.dataSource = self;
    
    //注册cell
    UINib *nib = [UINib nibWithNibName:@"QDDisComplicationCell" bundle:nil];
    [tableView registerNib:nib forCellReuseIdentifier:@"disComCell"];

#pragma mark -- 导航栏
    self.title = @"并发症";
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(clickConfirm:)];
    [rightBtn setTintColor:[UIColor whiteColor]];
    self.navigationItem.rightBarButtonItem = rightBtn;
}

//点击确定返回
- (void)clickConfirm:(NSArray<NSString*>*)strArray
{
    //已订阅的并发症
    NSMutableArray<NSString*> *orderArray = [[NSMutableArray alloc]init];
    for (QDDisComplicationModel *model in self.modelList) {
        if (model.isSelected == YES) {
            [orderArray addObject:model.complication_name];
        }
    }
    //传值
    if (_backBlock) {
        _backBlock(orderArray.copy);
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark
#pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.modelList.count;
}


- (QDDisComplicationCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    QDDisComplicationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"disComCell" forIndexPath:indexPath];
    cell.model = self.modelList[indexPath.row];
        return cell;
}



@end
